package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysGeneralSurvey;
import com.ruoyi.system.mapper.SysGeneralnaireRecordMapper;
import com.ruoyi.system.domain.SysGeneralnaireRecord;
import com.ruoyi.system.service.ISysGeneralnaireRecordService;

/**
 * 普查结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-11
 */
@Service
public class SysGeneralnaireRecordServiceImpl implements ISysGeneralnaireRecordService 
{
    @Autowired
    private SysGeneralnaireRecordMapper sysGeneralnaireRecordMapper;

    /**
     * 查询普查结果
     * 
     * @param questionId 普查结果主键
     * @return 普查结果
     */
    @Override
    public SysGeneralnaireRecord selectSysGeneralnaireRecordById(Long questionId,Long companyId,Long userId)
    {
        return sysGeneralnaireRecordMapper.selectSysGeneralnaireRecordById(questionId,companyId,userId);
    }

    /**
     * 查询普查结果列表
     * 
     * @param sysGeneralnaireRecord 普查结果
     * @return 普查结果
     */
    @Override
    public List<SysGeneralnaireRecord> selectSysGeneralnaireRecordList(SysGeneralnaireRecord sysGeneralnaireRecord)
    {
        return sysGeneralnaireRecordMapper.selectSysGeneralnaireRecordList(sysGeneralnaireRecord);
    }

    /**
     * 新增普查结果
     * 
     * @param sysGeneralnaireRecord 普查结果
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysGeneralnaireRecord(SysGeneralnaireRecord sysGeneralnaireRecord)
    {
        sysGeneralnaireRecord.setCreateTime(DateUtils.getNowDate());
        SysGeneralnaireRecord tsysGeneralnaireRecord=sysGeneralnaireRecordMapper.selectSysGeneralnaireRecordById(sysGeneralnaireRecord.getQuestionId(),sysGeneralnaireRecord.getCompanyId(), sysGeneralnaireRecord.getUserId());
        int rows;
        if(StringUtils.isNotNull(tsysGeneralnaireRecord)){
             rows = sysGeneralnaireRecordMapper.updateSysGeneralnaireRecord(sysGeneralnaireRecord);
        }else{
             rows = sysGeneralnaireRecordMapper.insertSysGeneralnaireRecord(sysGeneralnaireRecord);
        }

        return rows;
    }

    /**
     * 修改普查结果
     * 
     * @param sysGeneralnaireRecord 普查结果
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysGeneralnaireRecord(SysGeneralnaireRecord sysGeneralnaireRecord)
    {
        sysGeneralnaireRecord.setUpdateTime(DateUtils.getNowDate());

        SysGeneralnaireRecord tsysGeneralnaireRecord=sysGeneralnaireRecordMapper.selectSysGeneralnaireRecordById(sysGeneralnaireRecord.getQuestionId(),sysGeneralnaireRecord.getCompanyId(), sysGeneralnaireRecord.getUserId());
        int rows;
        if(StringUtils.isNotNull(tsysGeneralnaireRecord)){
            rows = sysGeneralnaireRecordMapper.updateSysGeneralnaireRecord(sysGeneralnaireRecord);
        }else{
            rows = sysGeneralnaireRecordMapper.insertSysGeneralnaireRecord(sysGeneralnaireRecord);
        }
        return rows;
    }

    /**
     * 批量删除普查结果
     * 
     * @param questionIds 需要删除的普查结果主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysGeneralnaireRecordByQuestionIds(Long[] questionIds)
    {

        return sysGeneralnaireRecordMapper.deleteSysGeneralnaireRecordByQuestionIds(questionIds);
    }

    /**
     * 删除普查结果信息
     * 
     * @param questionId 普查结果主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysGeneralnaireRecordByQuestionId(Long questionId)
    {

        return sysGeneralnaireRecordMapper.deleteSysGeneralnaireRecordByQuestionId(questionId);
    }

    /**
     * 新增普查问卷问题信息
     * 
     * @param sysGeneralnaireRecord 普查结果对象
     */
    public void insertSysGeneralSurveyRecord(SysGeneralnaireRecord sysGeneralnaireRecord)
    {
        List<SysGeneralSurvey> sysGeneralSurveyList = sysGeneralnaireRecord.getSysGeneralSurveyList();
        Long questionId = sysGeneralnaireRecord.getQuestionId();
        if (StringUtils.isNotNull(sysGeneralSurveyList))
        {
            List<SysGeneralSurvey> list = new ArrayList<SysGeneralSurvey>();
            for (SysGeneralSurvey sysGeneralSurvey : sysGeneralSurveyList)
            {
                sysGeneralSurvey.setQuestionId(questionId);
                list.add(sysGeneralSurvey);
            }
            if (list.size() > 0)
            {
                sysGeneralnaireRecordMapper.batchSysGeneralSurveyRecords(list);
            }
        }
    }
}
